<?php
declare (strict_types = 1);

namespace app\adminapi\controller;

use app\adminapi\business\TypeBusiness;
use app\adminapi\model\pygType;
use think\Exception;
use think\Request;

class type
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        try {
            $data = pygType::with(['attr','spec'])->select();
            return success(200,'ok',$data);
        }catch (\Exception $exception){
            throw new Exception($exception->getMessage());
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = \request()->all();
        foreach ($data['spec'] as $key =>$value){
            if (empty(trim($value['name']))){
                unset($data['spec'][$key]);
                continue;
            }
        }
        foreach ($data['spec'][$key]['value'] as $k=>$v){
            if (empty(trim($v))){
               unset($data['spec']['key']);
            }
        }
        TypeBusiness::save($data);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        try {
            if (!is_numeric($id)){
                throw new Exception('删除的参数不正确');
            }
            $res = pygType::where('id',$id)->find();
        }catch (\Exception $exception){
            return error($exception->getCode(),$exception->getMessage());
        }

    }
}
